package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.DictItemPO;
import com.falcon.domain.dos.DictItemDO;
import com.falcon.domain.req.DictItemQuery;
import com.falcon.domain.req.DictItemRequest;

import com.falcon.mapper.IDictItemMapper;
import com.falcon.service.IDictItemService;


@Slf4j
@Service
public class DictItemService implements IDictItemService {

    @Autowired
    private IDictItemMapper dictItemMapper;

    @Override
    public Long save(DictItemRequest request){
        DictItemPO dictItemPO = BeanUtil.copyProperties(request, DictItemPO.class);

        this.dictItemMapper.insertNotNull(dictItemPO);

        return dictItemPO.getId();
    }

    @Override
    public Boolean update(Long id, DictItemRequest request){

        DictItemPO dictItemPO = BeanUtil.copyProperties(request, DictItemPO.class);
        dictItemPO.setId(id);

        return this.dictItemMapper.updateNotNull(dictItemPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.dictItemMapper.deleteById(id);
    }

    @Override
    public DictItemDO queryById(Long id){

        DictItemPO dictItemPO = this.dictItemMapper.selectById(id);

        DictItemDO dictItemDO = BeanUtil.copyProperties(dictItemPO, DictItemDO.class);

        return dictItemDO;
    }


    @Override
    public List<DictItemDO> queryList(DictItemQuery request){

        Condition condition = request.toCondition();

        List<DictItemPO> poList = this.dictItemMapper.selectList(condition);

        List<DictItemDO> doList = BeanUtil.copyToList(poList, DictItemDO.class);

        return doList;
    }

    @Override
    public BasePager<DictItemDO> queryPager(DictItemQuery request){


        PageInfo<DictItemPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.dictItemMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, DictItemDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.dictItemMapper.deleteByIds(ids);
    }


}
